package com.lutzyt.delivery.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutzyt.delivery.bean.Commodity;
import com.lutzyt.delivery.bean.CommodityComment;
import com.lutzyt.delivery.bean.HorsemanComment;
import com.lutzyt.delivery.mapper.CommodityCommentDAO;
import com.lutzyt.delivery.service.CommentService;
import com.lutzyt.delivery.service.CommodityCommentService;
import com.lutzyt.delivery.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zyt
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CommodityCommentServiceImpl extends ServiceImpl<CommodityCommentDAO, CommodityComment> implements CommodityCommentService {

  private CommodityCommentDAO commodityCommentDAO;
  private CommodityService commodityService;

  @Transactional(readOnly = true)
  @Override
  public CommodityComment findById(Integer id) {
    return commodityCommentDAO.selectById(id);
  }

  @Transactional(readOnly = true)
  @Override
  public IPage<CommodityComment> findByPage(int pageNum, int pageSize) {
    return commodityCommentDAO.selectPage(new Page<>(pageNum, pageSize), null);
  }

  @Override
  public void insert(CommodityComment commodityComment) {
    commodityCommentDAO.insert(commodityComment);
  }

  @Override
  public void update(CommodityComment commodityComment) {
    commodityCommentDAO.updateById(commodityComment);
  }

  @Override
  public void deleteById(Integer id) {
    commodityCommentDAO.deleteById(id);
  }

  @Override
  public List<CommodityComment> findByCustomerId(Integer id) {
    final List<CommodityComment> list = this.lambdaQuery()
        .eq(CommodityComment::getCustomerId, id)
        .eq(CommodityComment::getDeleted, 0)
        .list();
    list.forEach(this::setComment);
    return list;
  }

  @Override
  public List<CommodityComment> getByCommodityId(Integer id) {
    final List<CommodityComment> list = this.lambdaQuery()
        .eq(CommodityComment::getCommodityId, id)
        .eq(CommodityComment::getDeleted, 0)
        .eq(CommodityComment::getState, "PASS")
        .list();
    return list;
  }

  public void setComment(CommodityComment commodityComment){
    final Commodity commodity = commodityService.findById(commodityComment.getCommodityId());
    commodityComment.setCommodity(commodity);
  }

  @Override
  public String getAuditField() {
    return "state";
  }

  @Autowired
  public void setCommodityService(CommodityService commodityService) {
    this.commodityService = commodityService;
  }

  @Autowired
  public void setCommodityCommentDAO(CommodityCommentDAO commodityCommentDAO) {
    this.commodityCommentDAO = commodityCommentDAO;
  }
}